/*-----------------------------------------------------------------------------

 Copyright 2017 Hopsan Group

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.


 The full license is available in the file LICENSE.
 For details about the 'Hopsan Group' or information about Authors and
 Contributors see the HOPSANGROUP and AUTHORS files that are located in
 the Hopsan source code root directory.

-----------------------------------------------------------------------------*/

#ifndef SIGNALATTITUDETVCCONTROL_HPP_INCLUDED
#define SIGNALATTITUDETVCCONTROL_HPP_INCLUDED

#include <iostream>
#include "ComponentEssentials.h"
#include "ComponentUtilities.h"
#include "math.h"

//!
//! @file SignalAttitudeTVCcontrol.hpp
//! @author Petter Krus <petter.krus@liu.se>
//  co-author/auditor **Not yet audited by a second person**
//! @date Tue 14 Apr 2015 16:48:37
//! @brief Attitude control unit for an aircraft
//! @ingroup SignalComponents
//!
//==This code has been autogenerated using Compgen==
//from 
/*{, C:, HopsanTrunk, componentLibraries, defaultLibrary, Signal, \
Control}/SignalControlAero.nb*/

using namespace hopsan;

class SignalAttitudeTVCcontrol : public ComponentSignal
{
private:
     double Kelev;
     double Krud;
     double KQrud;
     double KRrud;
     double umin;
     double umax;
     int mNstep;
//==This code has been autogenerated using Compgen==
     //inputVariables
     double thetaref;
     double psiref;
     double phi;
     double theta;
     double psi;
     double Qb;
     double Rb;
     //outputVariables
     double uelev;
     double urud;
     //Expressions variables
     //Delay declarations
//==This code has been autogenerated using Compgen==
     //inputVariables pointers
     double *mpthetaref;
     double *mppsiref;
     double *mpphi;
     double *mptheta;
     double *mppsi;
     double *mpQb;
     double *mpRb;
     //inputParameters pointers
     double *mpKelev;
     double *mpKrud;
     double *mpKQrud;
     double *mpKRrud;
     double *mpumin;
     double *mpumax;
     //outputVariables pointers
     double *mpuelev;
     double *mpurud;
     EquationSystemSolver *mpSolver;

public:
     static Component *Creator()
     {
        return new SignalAttitudeTVCcontrol();
     }

     void configure()
     {
//==This code has been autogenerated using Compgen==

        mNstep=9;

        //Add ports to the component
        //Add inputVariables to the component
            addInputVariable("thetaref","Reference signal \
tip","rad",0.,&mpthetaref);
            addInputVariable("psiref","Reference signal \
yaw","rad",0.,&mppsiref);
            addInputVariable("phi","roll angle","rad",0.,&mpphi);
            addInputVariable("theta","tipp angle","rad",0.,&mptheta);
            addInputVariable("psi","yaw angle","rad",0.,&mppsi);
            addInputVariable("Qb","tip angle rate","rad/s",0.,&mpQb);
            addInputVariable("Rb","yaw angle rate","rad/s",0.,&mpRb);

        //Add inputParammeters to the component
            addInputVariable("Kelev", "Gain tip, default", "rad", \
4.,&mpKelev);
            addInputVariable("Krud", "Gain yaw, default", "rad", 1.,&mpKrud);
            addInputVariable("KQrud", "Gain tip rate, default", "", \
1.,&mpKQrud);
            addInputVariable("KRrud", "Gain yaw rate, default", "", \
1.,&mpKRrud);
            addInputVariable("umin", "Minium output signal roll", "rad", \
-0.9,&mpumin);
            addInputVariable("umax", "Maximum output signal roll", "rad", \
0.9,&mpumax);
        //Add outputVariables to the component
            addOutputVariable("uelev","elevator","rad",0.,&mpuelev);
            addOutputVariable("urud","rudder","rad",0.,&mpurud);

//==This code has been autogenerated using Compgen==
        //Add constantParameters
     }

    void initialize()
     {
        //Read port variable pointers from nodes

        //Read variables from nodes

        //Read inputVariables from nodes
        thetaref = (*mpthetaref);
        psiref = (*mppsiref);
        phi = (*mpphi);
        theta = (*mptheta);
        psi = (*mppsi);
        Qb = (*mpQb);
        Rb = (*mpRb);

        //Read inputParameters from nodes
        Kelev = (*mpKelev);
        Krud = (*mpKrud);
        KQrud = (*mpKQrud);
        KRrud = (*mpKRrud);
        umin = (*mpumin);
        umax = (*mpumax);

        //Read outputVariables from nodes
        uelev = (*mpuelev);
        urud = (*mpurud);

//==This code has been autogenerated using Compgen==


        //Initialize delays


        simulateOneTimestep();

     }
    void simulateOneTimestep()
     {
        //Read variables from nodes

        //Read inputVariables from nodes
        thetaref = (*mpthetaref);
        psiref = (*mppsiref);
        phi = (*mpphi);
        theta = (*mptheta);
        psi = (*mppsi);
        Qb = (*mpQb);
        Rb = (*mpRb);

        //Read inputParameters from nodes
        Kelev = (*mpKelev);
        Krud = (*mpKrud);
        KQrud = (*mpKQrud);
        KRrud = (*mpKRrud);
        umin = (*mpumin);
        umax = (*mpumax);

        //LocalExpressions

          //Expressions
          uelev = limit(Kelev*(-(KQrud*Qb) + \
diffAngle(thetaref,theta)),umin,umax);
          urud = limit(Krud*(-(KRrud*Rb) + diffAngle(psiref,psi)),umin,umax);

        //Calculate the delayed parts


        //Write new values to nodes
        //outputVariables
        (*mpuelev)=uelev;
        (*mpurud)=urud;

        //Update the delayed variabels

     }
    void deconfigure()
    {
        delete mpSolver;
    }
};
#endif // SIGNALATTITUDETVCCONTROL_HPP_INCLUDED
